package com.rbac.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rbac.pojo.Permission;
import com.rbac.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Delete("delete from role_user where role_id = #{roleId} ")
    Integer removeRoleUser(Integer roleId);

    @Delete("delete from role_permission where role_id = #{roleId} ")
    Integer removeRolePermission(Integer roleId);

    // 批量删除角色表数据demo测试
    @Delete("<script>" +
            "DELETE FROM role WHERE role_id IN " +
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>" +
            "#{roleId}" +
            "</foreach>" +
            "</script>")
    int deleteRoles(@Param("roleIds") List<Integer> roleIds);

    @Select("select user_id from role_user where role_id = #{roleId}")
    List<Integer> queryRoleUserByRoleId(Integer roleId);

    @Select("select permission_id from role_permission where role_id = #{roleId}")
    List<Integer> queryRolePermissionByRoleId(Integer roleId);


    @Insert("insert into role_permission (role_id, permission_id) values (#{roleId}, #{permissionId})")
    Integer addRolePermission(@Param("roleId") Integer roleId,@Param("permissionId") Integer permissionId);

    @Select("select nickname from user where user_id in (select user_id from role_user where role_id = #{roleId})")
    List<String> queryUserNameListByRoleId(Integer roleId);

    @Select("select permission_name from permission where permission_id in (select permission_id from role_permission where role_id = #{roleId})")
    List<String> queryPermissionNameListByRoleId(Integer roleId);

    //获取用户的权限对象列表
    @Select("select * from permission where permission_id in (select permission_id from role_permission where role_id = #{roleId})")
    List<Permission> getUserPermission(Integer roleId);
}
